﻿<template>
    <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1300"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            autocomplete="off"
            label-width="100px"
        >
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="账号" prop="accountCode">
                        <el-input v-model="entityObj.accountCode" :disabled="mode != 'Add'" />
                    </el-form-item>
                </el-col>
                <el-col :span="12" v-if="entityObj.accountType==2">
                    <el-form-item label-width="120px" label="门店" prop="pId">
                        <sc-store-select
                            ref="orgselect"
                            :props="storeProps"
                            :selectKeyValue="'storeId'"
                            :multiple="false"
                            v-model="entityObj.store"
                            style="width: 100%;"
                            @fatherMethod="storeSelectProcess"
                        ></sc-store-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12" v-else-if="entityObj.accountType==3">
                    <el-form-item label-width="120px" label="供应商" prop="pId">
                        <sc-supplier-select
                            ref="orgselect"
                            :props="supplierProps"
                            :selectKeyValue="'supplierId'"
                            :multiple="false"
                            v-model="entityObj.supplier"
                            style="width: 100%;"
                            @fatherMethod="supplierSelectProcess"
                        ></sc-supplier-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12" v-else-if="entityObj.accountType==1||entityObj.accountType==4">
                    <el-form-item label-width="120px" label="采购单位" prop="pId">
                        <sc-purchase-unity-select
                            ref="orgselect"
                            :props="purchaseUnityProps"
                            :selectKeyValue="'purchaseUnityId'"
                            :multiple="false"
                            v-model="entityObj.purchaseUnity"
                            style="width: 100%;"
                            @fatherMethod="purchaseUnitySelectProcess"
                        ></sc-purchase-unity-select>
                    </el-form-item>
                </el-col>
                <!-- <el-col :span="12" v-else>
                    <el-form-item label-width="120px" label="分配门店" prop="stores">
                        <sc-store-select
                            ref="orgselect"
                            :props="storeProps"
                            :selectKeyValue="'storeId'"
                            multiple
                            v-model="entityObj.stores"
                            @fatherMethod="storesSelectProcess"
                            style="width: 100%;"
                        ></sc-store-select>
                    </el-form-item>
                </el-col>-->
            </el-row>
            <el-row v-if="mode=='Add'">
                <el-col :span="12">
                    <el-form-item label-width="120px" label="密码" prop="password">
                        <el-input v-model="entityObj.password" show-password autocomplete="off" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="确认密码" prop="confirmPassword">
                        <el-input
                            v-model="entityObj.confirmPassword"
                            show-password
                            autocomplete="off"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系人" prop="realName">
                        <el-input v-model="entityObj.realName" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="手机号" prop="mobile">
                        <el-input v-model="entityObj.mobile" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="金蝶编号" >
                        <el-input v-model="entityObj.jdAccountCode" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="是否管理员账号" prop="isAdmin">
                        <el-switch
                            v-model="entityObj.isAdmin"
                            :active-value="true"
                            :inactive-value="false"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="分配角色">
                        <el-select
                            placeholder="---请选择角色---"
                            v-model="entityObj.roleIds"
                            style="width: 300px"
                            multiple
                        >
                            <el-option
                                v-for="item in optionsRoles"
                                :key="item.roleId"
                                :label="item.roleName"
                                :value="item.roleId"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="状态" prop="status">
                        <el-switch
                            v-model="entityObj.status"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="entityObj.accountType==1||entityObj.accountType==4">
                <el-col :span="12">
                    <el-form-item label-width="110px" label="分配要货客户" prop="applyOrderItems">
                        <el-button size="small" @click="addApplyOrg()">分配要货客户</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row v-if="entityObj.accountType==1||entityObj.accountType==4">
                <el-col :span="24">
                    <el-form-item label-width="110px">
                        <el-table :data="entityObj.stores" border style="width: 100%" show-summary>
                            <el-table-column label="机构类型" min-width="110">
                                <template #default="scope">
                                    <span v-if="scope.row.storeOrgType==1">供货单位</span>
                                    <span v-else-if="scope.row.storeOrgType==2">门店</span>
                                    <span v-else-if="scope.row.storeOrgType==3">供应商</span>
                                </template>
                            </el-table-column>
                            <el-table-column label="机构名称" min-width="260">
                                <template #default="scope">{{scope.row.storeOrgName}}</template>
                            </el-table-column>
                            <el-table-column label="操作" width="80">
                                <template #default="scope">
                                    <el-button
                                        size="small"
                                        @click="deleteRow(scope.$index, scope.row)"
                                    >移除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>

        <template>
            <full-screen-dialog
                v-model="applyOrgState"
                title="选择客户"
                :width="1300"
                :center="true"
                append-to-body
                style="height: 480px"
            >
                <el-row>
                    <sc-apply-org-choice
                        ref="scApplyOrgChoice"
                        :defaultQueryList="applyOrgQueryList"
                    ></sc-apply-org-choice>
                </el-row>
                <template #footer>
                    <el-button @click="applyOrgState = false">取 消</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="setApplyOrgChoice">确定</el-button>
                </template>
            </full-screen-dialog>
        </template>
    </el-dialog>
</template>

<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                status: 1,
                isAdmin: false,
            },
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            storeProps: {
                label: "storeName",
                value: "storeId",
            },
            supplierProps: {
                label: "supplierName",
                value: "supplierId",
            },
            purchaseUnityProps: {
                label: "purchaseUnityName",
                value: "purchaseUnityId",
            },
            purchaseUnitys: [],
            optionsPurchaseDept: [],
            optionsRoles: [],
            applyOrgState: false,
            applyOrgQueryList: {},
            //验证规则
            rules: {
                accountCode: [
                    { required: true, type: "string", message: "请填写" },
                ],
                password: [
                    { required: true, type: "string", message: "请填写" },
                ],
                confirmPassword: [
                    { required: true, type: "string", message: "请填写" },
                ],
                pid: [{ required: true, type: "number", message: "请填写" }],
                realName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                mobile: [{ required: true, type: "string", message: "请填写" }],
                status: [{ required: true, type: "number", message: "请填写" }],
            },
        };
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    try {
                        if (
                            this.entityObj.password !=
                            this.entityObj.confirmPassword
                        ) {
                            this.$message.success("输入的密码不一致");
                            return false;
                        }
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                this.mode == "Add"
                                    ? "account/create"
                                    : "account/update",
                                { ...this.entityObj }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.visible = false;
                            this.isSaveing = false;
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        this.visible = false;
                        this.isSaveing = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(data) {
            this.entityObj = { ...data };
            this.entityObj.pId = tool.data.get("USER_INFO").OrgId;

            if (this.entityObj.accountType == 2) {
                this.entityObj.store = {
                    storeId: this.entityObj.pId,
                    storeName: this.entityObj.orgName,
                };
            }

            if (this.entityObj.accountType == 3) {
                this.entityObj.supplier = {
                    supplierId: this.entityObj.pId,
                    supplierName: this.entityObj.orgName,
                };
            }

            if (this.entityObj.accountType == 1||this.entityObj.accountType == 4) {
                this.entityObj.purchaseUnity = {
                    purchaseUnityId: this.entityObj.pId,
                    purchaseUnityName: this.entityObj.orgName,
                };
            }

            if ((data.accountType == 1||this.entityObj.accountType == 4) && data.accountId) {
                //账号店铺权限
                var returnValue = await this.$API.auth.GetProcessApi.get(
                    "account/auth-store/" + data.accountId,
                    {}
                );
                if (returnValue.status == 200) {
                    this.entityObj.stores = returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            }

            var returnValue = await this.$API.auth.PostProcessApi.post(
                "role/list",
                { orgType: data.accountType }
            );
            if (returnValue.status == 200) {
                this.optionsRoles = returnValue.response;
            } else {
                this.$message.error("操作失败:" + returnValue.msg);
            }
        },
        async storeSelectProcess(val) {
            console.log(val);
            this.entityObj.pId = val.storeId;
            this.entityObj.store = val;
        },
        async supplierSelectProcess(val) {
            this.entityObj.pId = val.supplierId;
            this.entityObj.supplier = val;
        },
        async purchaseUnitySelectProcess(val) {
            this.entityObj.pId = val.purchaseUnityId;
            this.entityObj.purchaseUnity = val;
        },
        // async storesSelectProcess(rowData) {
        //     this.entityObj.stores = [];
        //     this.entityObj.storeIds = [];
        //     if (rowData && rowData.length > 0) {
        //         rowData.map((p, index) => {
        //             if (p) {
        //                 this.entityObj.stores = [
        //                     ...this.entityObj.stores,
        //                     {
        //                         storeId: p.storeId,
        //                         storeName: p.storeName,
        //                     },
        //                 ];
        //                 this.entityObj.storeIds = [
        //                     ...this.entityObj.storeIds,
        //                     p.storeId,
        //                 ];
        //             }
        //         });
        //     }
        // },
        //获取要货客户
        addApplyOrg() {
            // const supplierIds = this.entityObj.stores.filter(t=>t.storeOrgType==3).map((item) => item.storeId);
            // this.supplierQueryList = {
            //     //isAll: true,
            //     notSupplierIds: supplierIds,
            // };
            this.applyOrgState = true;
            this.$nextTick(() => {
                this.$refs.scApplyOrgChoice.opBindData();
            });
        },
        //设置要货客户
        setApplyOrgChoice() {
            if(!this.entityObj.stores||this.entityObj.stores.length==0){
                this.entityObj.stores=[];
            }
            let applyOrgs = this.$refs.scApplyOrgChoice.selectionRows;
            applyOrgs.map((p) => {
                if (
                    !this.entityObj.stores.some(
                        (t) =>
                            t.storeOrgType == p.storeOrgType &&
                            t.storeId == p.storeId
                    )
                ) {
                    this.entityObj.stores.push({
                        storeOrgType: p.storeOrgType,
                        storeId: p.storeId,
                        storeOrgName: p.storeOrgName,
                    });
                }
            });
            this.applyOrgState = false;
        },
        //删除
        deleteRow(index) {
            this.entityObj.stores.splice(index, 1);
        },
        async initOptions() {},
    },
    async mounted() {
        //this.entityObj.orgName=tool.data.get("USER_INFO").OrgName;
        await this.initOptions();
    },
};
</script>

<style></style>
